package com.gemme.gemme.pop;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gemme.gemme.R;
import com.gemme.gemme.view.OnChangeLIstener;
import com.gemme.gemme.view.RoundCheckBox;

public class CouponPopUtil extends CorePopUtil {

    private Context mContext;
    private boolean isCoupon = false;
    private PopClickInterface popClickInterface;

    /**
     * @param context 上下文
     */
    public CouponPopUtil(View view, Context context) {
        super(view, context, R.layout.view_pop_coupon_choice);
        this.mContext = context;
        initLayout(mView, mContext);
    }

    public void setPopClickInterface(PopClickInterface popClickInterface) {
        this.popClickInterface = popClickInterface;
    }

    public interface PopClickInterface {
        void enterClick(boolean isCoupon);
    }

    @Override
    public void initLayout(View view, final Context context) {
        LinearLayout btn_close = view.findViewById(R.id.btn_close);
        RoundCheckBox cb_syyhj = view.findViewById(R.id.cb_syyhj);
        RoundCheckBox cb_bsyyhj = view.findViewById(R.id.cb_bsyyhj);
        TextView btn_enter = view.findViewById(R.id.btn_enter);
        cb_syyhj.setChecked(false);
        cb_bsyyhj.setChecked(true);

        btn_close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismissPop();
            }
        });

        cb_syyhj.setOnChangeLIstener(new OnChangeLIstener() {
            @Override
            public void onChangeLIstener() {
                boolean checked = cb_syyhj.isChecked();
                isCoupon = checked;
                setCheckBoxData(isCoupon, cb_syyhj, cb_bsyyhj);
            }
        });

        cb_bsyyhj.setOnChangeLIstener(new OnChangeLIstener() {
            @Override
            public void onChangeLIstener() {
                boolean checked = cb_bsyyhj.isChecked();
                isCoupon = !checked;
                setCheckBoxData(isCoupon, cb_syyhj, cb_bsyyhj);
            }
        });

        btn_enter.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismissPop();
                if (popClickInterface != null) {
                    popClickInterface.enterClick(isCoupon);
                }
            }
        });
    }

    /**
     * 设置优惠券选择数据
     *
     * @param isCoupon
     * @param syyhjBox
     * @param bsyyhjBox
     */
    private void setCheckBoxData(boolean isCoupon, RoundCheckBox syyhjBox, RoundCheckBox bsyyhjBox) {
        if (isCoupon) {
            syyhjBox.setChecked(true);
            bsyyhjBox.setChecked(false);
        } else {
            syyhjBox.setChecked(false);
            bsyyhjBox.setChecked(true);
        }
    }
}